//
//  RootViewController.m
//  Find
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 Reiko. All rights reserved.
//

#import "RootViewController.h"
#import "HotViewController.h"
#import "FindViewController.h"
#import "LostViewController.h"
#import "MineViewController.h"
#import "PublishViewController.h"
#import "LoginViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface RootViewController ()<CLLocationManagerDelegate>

@property (nonatomic, retain)CLLocationManager *locationManager;

@end

@implementation RootViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NOTIFICATIONONE" object:nil];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViewControllers];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationInfo:) name:@"NOTIFICATIONONE" object:nil];
    //[self getLocation];
    self.selectedIndex = 1;   // 更改数字初始界面
}
- (void)getLocation
{
    // 定位管理器
    self.locationManager = [[CLLocationManager alloc] init];
    if (![CLLocationManager locationServicesEnabled]) {
        return;
    }
    // 如果没有授权则请求用户授权
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
        [self.locationManager requestAlwaysAuthorization];
    } else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways){
        self.locationManager.delegate = self;
        //设置定位精度
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        // 定位频率
        CLLocationDistance distance = 300;
        self.locationManager.distanceFilter = distance;
        // 启动跟踪器
        [self.locationManager startUpdatingLocation];
    }
}

#pragma mark 跟踪定位代理方法，每次位置发生变化即会执行（只要定位到相应位置）
//可以通过模拟器设置一个虚拟位置，否则在模拟器中无法调用此方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *location=[locations firstObject];//取出第一个位置
    CLLocationCoordinate2D coordinate=location.coordinate;//位置坐标
    //如果不需要实时定位，使用完即使关闭定位服务
    AVUser *user= [AVUser currentUser];
    [user setObject:[NSNumber numberWithFloat:coordinate.longitude] forKey:@"longitude"];
    [user setObject:[NSNumber numberWithFloat:coordinate.latitude] forKey:@"latitude"];
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [self.locationManager stopUpdatingLocation];
        } else {
        }
    }];
    
}
- (void)setupViewControllers {
  
    HotViewController *hotVC = [[HotViewController alloc] init];
    UIViewController *hotNC = [[UINavigationController alloc] initWithRootViewController:hotVC];
    
    LostViewController *lostVC = [[LostViewController alloc] init];
    UIViewController *lostNC = [[UINavigationController alloc] initWithRootViewController:lostVC];
    
    FindViewController *findVC = [[FindViewController alloc] init];
    UIViewController *findNC = [[UINavigationController alloc] initWithRootViewController:findVC];
    
    MineViewController *mineVC = [[MineViewController alloc] init];
    UIViewController *mineNC = [[UINavigationController alloc] initWithRootViewController:mineVC];
    [self customizeTabBarForController:self];
    [self setViewControllers:@[hotNC, lostNC, findNC, mineNC]];
}

- (void)customizeTabBarForController:(CYLTabBarController *)tabBarController {
    
    NSDictionary *dict1 = @{CYLTabBarItemTitle : @"热门",CYLTabBarItemImage : @"star_unselected.png",CYLTabBarItemSelectedImage : @"star_selected.png",};
    
    NSDictionary *dict2 = @{ CYLTabBarItemTitle : @"丢失",CYLTabBarItemImage : @"wallet_unselected.png",CYLTabBarItemSelectedImage : @"wallet_selected.png",};
    
    NSDictionary *dict3 = @{CYLTabBarItemTitle : @"发现", CYLTabBarItemImage : @"eye_unselected.png",CYLTabBarItemSelectedImage : @"eye_selected", };
    
    NSDictionary *dict4 = @{CYLTabBarItemTitle : @"我的",CYLTabBarItemImage : @"mine_unselected", CYLTabBarItemSelectedImage : @"mine_selected"};
    
    NSArray *tabBarItemsAttributes = @[ dict1, dict2, dict3, dict4 ];
    tabBarController.tabBarItemsAttributes = tabBarItemsAttributes;
}
// 添加消息模态出一个view
- (void)notificationInfo:(NSNotification *)notification
{
#pragma mark -- 判断是否登录 
    AVUser *user = [AVUser currentUser];
    if (!user) {
        LoginViewController  *loginVC = [[LoginViewController alloc] init];
        UINavigationController *naC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:naC animated:YES completion:nil];
    } else {
        
    NSString *typeButton =  [notification.userInfo valueForKey:@"type"];
    if ([typeButton isEqualToString:@"lost"]) {
        PublishViewController *publishVC = [[PublishViewController alloc] init];
        publishVC.publishInfoType = @"lost";
        UINavigationController *naC = [[UINavigationController alloc] initWithRootViewController:publishVC];
        [self presentViewController:naC animated:YES completion:nil];
        
    } else {
        PublishViewController *publishVC = [[PublishViewController alloc] init];
        publishVC.publishInfoType = @"find";
        UINavigationController *naC = [[UINavigationController alloc] initWithRootViewController:publishVC];
        [self presentViewController:naC animated:YES completion:nil];
    }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
